<?php



/**

 * @author ducnhat

 * @email ddnhat@gmail.com

 * @copyright 2014

 */

 

class SupportController extends Zenddn_Controller_Action{

    

    private $_settings = null;

    

    public function init(){

        parent::init();

        parent::loadTemplate('magashop');

        $this->view->page = "support";

        

        $setting_model = new Default_Model_Setting();

        $settings = $setting_model->getData('page_category');

        $this->_settings = unserialize($settings['value']);

        parent::addScripts(array('jquery-ui.js'));

    }

    

    public function indexAction(){

        $category = $this->_settings['support_page'];

        

        $this->view->data = $this->getPost($category);

        $this->view->page = "support";

    }

    

    public function installAction(){

    	$category = $this->_settings['install_page'];

        

        $this->view->data = $this->getPost($category);

        echo $this->view->render('support/index.phtml');

        $this->view->page = "support_install";

    }

    

    public function checkoutAction(){

    	$category = $this->_settings['checkout_page'];

        

        $this->view->data = $this->getPost($category);

        echo $this->view->render('support/index.phtml');

        $this->view->page = "support_checkout";

    }

    

    public function faqAction(){

    	$category = $this->_settings['faq_page'];

        

        $this->view->data = $this->getPost($category);

        echo $this->view->render('support/index.phtml');

        $this->view->page = "support_faq";

    }

    

    private function getPost($category){

    	$post_model = new Default_Model_Post();

        $data = $post_model->getDataByCategory($category);

        

        return $data;

    }
    
    public function getKeysAction(){
    	$this->view->page = "support_get_keys";
        
        $request = $this->_request;
        
        if($request->isPost()){
            $validator = new Default_Form_Order($request->getParams(), false);
            
            if($validator->isError()){
                $this->view->error = $validator->getMessageError();
            }else{
                $this->_helper->viewRenderer->setNoRender();
                
                $data = $validator->getData();
                
                $order_model = new Default_Model_Order();
                $orders = $order_model->getDataByEmail($data['email'], true);
                
                if($orders != null){
                    $items = array();
                    
                    $key_model = new Default_Model_ProductKey();
                    
                    foreach($orders as $order){
                        $items_temp = $key_model->getUsedKeys($order->id);
                        foreach($items_temp as $item){
                            $items[] = $item;
                        }
                    }
                    
                    $this->view->items = $items;
                    $this->view->name = $data['name'];
                    $content = $this->view->render("email/key.phtml");
                    
                    $mail = new Zenddn_Mail_Mail();
    	            $mail->sendProductKey($data['email'], $data['name'], $content);
                    
                    $title = "get keys success";
    	            $message = "get keys success message";
                }else{
                    $title = "get keys unsuccess";
    	            $message = "get keys unsuccess message";
                }
                
                $this->view->title = $title;
                $this->view->message = $message;
                
                echo $this->view->render('account/success.phtml');
            }
        }
    }
}